package com.wzw.skin_code;

import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Observable;

import dalvik.system.DexClassLoader;

/**
 * @author : wangzw
 * @date : 21-1-20上午11:17
 * @desc :
 */
public class SkinManager extends Observable {
    private static SkinManager instance;
    private Application application;
    private static final String TAG = SkinManager.class.getSimpleName();


    public static void init(Application application){
        if (null == instance){
            synchronized (SkinManager.class){
                if (null == instance){
                    instance = new SkinManager(application);
                }
            }
        }
    }
    public static SkinManager getInstance() {
        return instance;
    }

    public SkinManager(Application application) {
        this.application = application;
        application.registerActivityLifecycleCallbacks(new SkinActivityLifecycle());
    }


    public void loadSkin() {
        FileUtil.copyAssetAndWrite(application, "skin-release-unsigned.apk");
        File dataFile = new File(application.getCacheDir(), "skin-release-unsigned.apk");
        if (!dataFile.exists()) {
            return;
        }
        try {
            //获取assetManager
            AssetManager assetManager = AssetManager.class.newInstance();
            Method addAssetPath = AssetManager.class.getMethod("addAssetPath", String.class);
            addAssetPath.invoke(assetManager,dataFile.getAbsolutePath());

            //获取到插件resource
            Resources appResource = application.getResources();
            Resources skinResource = new Resources(assetManager,appResource.getDisplayMetrics(),appResource.getConfiguration());

            //获取插件进程名
            PackageManager packageManager = application.getPackageManager();
            PackageInfo packageArchiveInfo = packageManager.getPackageArchiveInfo(dataFile.getAbsolutePath(), PackageManager.GET_ACTIVITIES);
            String packageName = packageArchiveInfo.applicationInfo.packageName;

            //获取插件dexClassloader
            File optimizedDirectory = application.getDir("dex", Context.MODE_PRIVATE);
            DexClassLoader dexClassLoader = new DexClassLoader(dataFile.getAbsolutePath(),optimizedDirectory.getAbsolutePath(),
                    null,application.getClassLoader());

            SkinResource.getInstance().init(dexClassLoader,skinResource,appResource,packageName);


        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        setChanged();
        notifyObservers();
    }




}
